<template>
  <div class="container">
    <div class="app-container">
      <el-button class="btn-add" type="primary" size="mini" @click="addTotal()">
        添加权限
      </el-button>
      <el-table
        default-expand-all
        row-key="id"
        :data="tableData"
        style="width: 100%"
      >
        <el-table-column align="center" prop="name" label="名称" width="180">
        </el-table-column>
        <el-table-column align="center" prop="code" label="标识" width="180">
        </el-table-column>
        <el-table-column align="center" prop="description" label="概述">
        </el-table-column>
        <el-table-column align="center" label="操作">
          <template v-slot="{ row }">
            <el-button
              v-if="row.type === 1"
              type="text"
              size="mini"
              @click="addchildLevel(row.id)"
            >
              添加
            </el-button>
            <el-button
              type="text"
              size="mini"
              @click="quitPermissionFn(row.id)"
            >
              编辑
            </el-button>
            <el-button type="text" size="mini" @click="delPermissionFn(row.id)">
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 新增编辑弹层的组件 -->
    <addeditPermissionVue
      :dialogVisible.sync="dialogVisible"
      :fid="fid"
      :numType="numType"
      ref="childPer"
    >
    </addeditPermissionVue>
  </div>
</template>
<script>
import { getPermissionListApi, delPermissionApi } from "@/api/permission";
import { transListToTreeData } from "@/utils";
import addeditPermissionVue from "./components/addeditPermission.vue";
export default {
  name: "Permission",
  components: {
    addeditPermissionVue,
  },
  data() {
    return {
      tableData: [],
      dialogVisible: false, // 控制弹层组件的开关
      fid: null, // 父节点id，用作子节点的pid用  子节点pid = 父节点id
      numType: null, // 判断是一级还是二级
      id: null, // 详情用的id
    };
  },
  created() {
    this.getPermissionListFn();
  },
  methods: {
    async getPermissionListFn() {
      const res = await getPermissionListApi();
      this.tableData = transListToTreeData(res, 0);
    },
    // 打开添加权限弹层  总的
    addTotal() {
      this.dialogVisible = true;
      this.fid = 0;
      this.numType = 1;
    },
    // 打开添加权限弹层  二级的
    addchildLevel(id) {
      this.dialogVisible = true;
      this.fid = id;
      this.numType = 2;
    },
    // 编辑权限
    quitPermissionFn(id) {
      // this.loading = true;
      this.$refs.childPer.detaPermissionApi(id);
      setTimeout(() => {
        this.dialogVisible = true;
      }, 800);
    },
    // 删除权限
    delPermissionFn(id) {
      this.$confirm("确认删除此权限点吗", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
      }).then(async () => {
        await delPermissionApi(id);
        this.getPermissionListFn();
        this.$message({
          type: "success",
          message: "删除成功!",
        });
      });
      // .catch(() => {
      //   this.$message({
      //     type: "info",
      //     message: "已取消删除",
      //   });
      // });
    },
  },
};
</script>
<style scoped>
.btn-add {
  margin: 20px 10px;
}
</style>
